package ylsb.controller;

import org.springframework.beans.factory.annotation.Value;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ylsb.service.*;
import org.springframework.web.bind.annotation.RestController;
import ylsb.model.*;
import ylsb.service.ShijiService;
import ylsb.service.ShijijieguoService;
import ylsb.service.YltpXiangmuService;
import ylsb.service.YltprenyuanfenpeiService;
import ylsb.support.JsonResult;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
@RequestMapping("shijijieguo")
public class ShijijieguoController {

    @Value("${userfilepath}")
    private String userfilepath;
    @Resource
    ShijiService ddShijiService;
    @Resource
    YltprenyuanfenpeiService ddYltprenyuanfenpeiService;
    @Resource
    YltpXiangmuService ddYltpXiangmuService;
    @Resource
    ShijijieguoService ddService;
    @Resource
    YltpuserService ddYltpuserService;
    @Resource
    DailiangchuangxinService ddDailiangchuangxinService;
    @Resource
    ShebeiService ddShebeiService;
    @Resource
    YltpjieguoService ddYltpjieguoService;
    @Resource
    PinpaiguanlianService ddPinpaiguanlianService;

    public Logger log = LoggerFactory.getLogger(YiyongijieguoController.class);

    @RequestMapping("getjindulist")
    public JsonResult<?> getjindulist(@RequestParam("xiangmuid") String ppxiangmuId,
                                      @RequestParam("xiangmuleixing") String ppxiangmuleixiang) {
        try {
            List<Map<String, Object>> mmJindulist = new ArrayList<>();
            Integer mmTotal = 0;
            if (("12").equals(ppxiangmuleixiang)) {
                mmTotal = ddShebeiService.getShebeiCount(ppxiangmuId);
            } else {
                Integer mmTotalall = ddShijiService.getPinguiCount(ppxiangmuId);
                Integer mmpaichu = ddShijiService.getPaichuPinguiCount(ppxiangmuId);
                mmTotal=mmTotalall-mmpaichu;
            }
            /*if (ppxiangmuleixiang.equals("8") || ppxiangmuleixiang.equals("9")) {
                mmTotal = ddDailiangchuangxinService.getChuangxinCount(ppxiangmuId);
            }*/

            List<Map<String, Object>> mmUserList = ddYltprenyuanfenpeiService.findRenyuanFenpeiByXiangmuId(ppxiangmuId);
            for (Map<String, Object> ysrenyuanfenpei : mmUserList) {
                Map<String, Object> mmMap = new HashMap<String, Object>();
                String mmUserId = ysrenyuanfenpei.get("userid").toString();
                YltpUser mmuser = ddYltpuserService.findOne(mmUserId);
                String mmUserName = mmuser.getUsername();
                Integer mmpinguishu = 0;
                if (("12").equals(ppxiangmuleixiang)) {
                    mmpinguishu = ddYltpjieguoService.getShebeiResultCount(ppxiangmuId, mmUserId, "1");
                }else {
                    mmpinguishu = ddService.getPinguiResultCount(ppxiangmuId, mmUserId, "1");
                }
                /*if (ppxiangmuleixiang.equals("8") || ppxiangmuleixiang.equals("9")) {
                    mmpinguishu = ddYltpjieguoService.getChuangxinchanpinResultCount(ppxiangmuId, mmUserId, "1");
                } */

                String mmShifoutijiao = "";
                if ("1".equals(ysrenyuanfenpei.get("issubmit").toString())) {
                    mmShifoutijiao = "是";
                } else {
                    mmShifoutijiao = "否";
                }
                mmMap.put("username", mmUserName);
                mmMap.put("pinguishu", mmpinguishu + "/" + mmTotal);
                mmMap.put("shifoutijiao", mmShifoutijiao);
                mmJindulist.add(mmMap);
            }

            return JsonResult.getSuccessResult(mmJindulist);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("shijijieguo/getjindulist:error", e);
            return JsonResult.getErrorResult("shijijieguo/getjindulist:error " + e.getMessage());
        }
    }

    @RequestMapping("submit")
    public JsonResult<?> submit(
            @RequestParam("xiangmuid") String ppxiangmuId,
            @RequestParam("userid") String ppuserId) {
        try {
            List<YltpRenyuanfenpei> list = ddYltprenyuanfenpeiService.findbyUserIDandXiangmuId(ppuserId, ppxiangmuId);
            if (list.size() < 1) {
                return JsonResult.getErrorResult("提交失败，人员-项目分配信息不存在");
            }
           /* Boolean isGuochan = false;
            String mmGuochanErr = "";
            Boolean isFengbi = false;
            String mmFengbiErr = "";
            List<Map<String, Object>> mmGuochanPinzhongList = ddShijiService.findGuochanPinzhong(ppxiangmuId);
            for (Map<String, Object> mmMap : mmGuochanPinzhongList) {
                String mmPinzhonxuhao = mmMap.get("pinzhongxuhao").toString();
                List<Map<String, Object>> mmPinzhongResultList = ddService.findPinzhongResult(ppxiangmuId, ppuserId, mmPinzhonxuhao);
                if (mmPinzhongResultList.size() != 0) {
                    List<Map<String, Object>> mmGuochanResultList = ddService.findResultGuochanBypinzhong(ppxiangmuId, ppuserId, mmPinzhonxuhao);
                    if (mmGuochanResultList.size() == 0) {
                        isGuochan = true;
                        mmGuochanErr = mmGuochanErr + "," + "【" + mmPinzhonxuhao + "】";
                    }
                }
            }
            if (isGuochan) {
                return JsonResult.getErrorResult("提交失败,品种序号" + mmGuochanErr + "有国产没有勾选。");
            }*/
           /* List<Map<String, Object>> mmFengbiPinzhongList = ddShijiService.findFengbi(ppxiangmuId);
            for (Map<String, Object> mmMap : mmFengbiPinzhongList) {
                String mmPinzhonxuhao = mmMap.get("pinzhongxuhao").toString();
                List<Map<String, Object>> mmFengbiCountList = ddShijiService.findFengbiCount(ppxiangmuId, mmPinzhonxuhao);
                List<Map<String, Object>> mmFengbiResultList = ddService.findFengbiResult(ppxiangmuId, ppuserId, mmPinzhonxuhao);
                if (mmFengbiCountList.size() != mmFengbiResultList.size()) {
                    isFengbi = true;
                    mmFengbiErr = mmFengbiErr + "," + "【" + mmPinzhonxuhao + "】";
                }
            }
            if (isFengbi) {
                return JsonResult.getErrorResult("提交失败,品种序号" + mmFengbiErr + "有封闭试剂没有勾选。");
            }*/

            YltpRenyuanfenpei mmRenyuanfenpei = new YltpRenyuanfenpei();
            mmRenyuanfenpei.setId(list.get(0).getId());
            mmRenyuanfenpei.setUserid(ppuserId);
            mmRenyuanfenpei.setXiangmuid(ppxiangmuId);
            mmRenyuanfenpei.setIssubmit(1);
            return ddYltprenyuanfenpeiService.update(mmRenyuanfenpei) == true ? JsonResult.getSuccessResult("提交成功")
                    : JsonResult.getErrorResult("提交失败");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("renyuanfenpei/submit:error", e);
            return JsonResult.getErrorResult("renyuanfenpei/submit:error " + e.getMessage());
        }

    }

    @RequestMapping("getResultCount")
    public JsonResult<?> getResultCount(@RequestParam("xiangmuid") String ppxiangmuId,
                                        @RequestParam("userid") String ppuserId) {
        try {
            Integer mmYiTongyishu1 = ddService.getPinguiResultCount("fe218a88-e5e8-408d-9921-ad91f359cd90", ppuserId, "1");
            Integer mmYiTongyishu2 = ddService.getPinguiResultCount("1f65ba47-af7b-401a-a881-b95ec1599358", ppuserId, "1");
            Integer mmYiTongyishu3 = ddService.getPinguiResultCount("55c361c4-a704-49bd-ac6e-7eb734c38001", ppuserId, "1");
            Integer mmYiTongyishu4 = ddService.getPinguiResultCount("d04fa580-8167-4b2a-9391-d59df4647b21", ppuserId, "1");
            Integer mmYiTongyishu5 = ddService.getPinguiResultCount("19e5c4ad-55e1-48ce-bb44-7b824c4ef324", ppuserId, "1");
            Integer mmYiTongyishu6 = ddService.getPinguiResultCount("05cffee2-4e29-4d75-a840-f6dc09fcf95b", ppuserId, "1");
            Integer mmYiTongyishu7 = ddService.getPinguiResultCount("43cef8c8-6f80-461c-a26d-3bbe67efc6b8", ppuserId, "1");
            Integer mmYiTongyishu = mmYiTongyishu1+mmYiTongyishu2+mmYiTongyishu3+mmYiTongyishu4+mmYiTongyishu5+mmYiTongyishu6+mmYiTongyishu7;
            /*Integer mmTongyishu = ddService.getPinguiResultCount(ppxiangmuId, ppuserId, "1");
            Integer mmTotal = ddShijiService.getPinguiCount(ppxiangmuId);
            Integer mmpaichu = ddShijiService.getPaichuPinguiCount(ppxiangmuId);
            Integer mmTotalpaichu=mmTotal-mmpaichu;
            Integer mmBuTongyishu = mmTotalpaichu - mmTongyishu;*/
            Integer mmTongyishu = ddService.getPinzhongResultCount(ppxiangmuId, ppuserId, "1");
            Integer mmTotal = ddShijiService.getPinzhongCount(ppxiangmuId);
            Integer mmBuTongyishu = mmTotal - mmTongyishu;
            Map<String, Object> mmMap = new HashMap<String, Object>();
            mmMap.put("yitongyiShu", mmYiTongyishu);
            mmMap.put("tongyiShu", mmTongyishu);
            mmMap.put("butongyiShu", mmBuTongyishu);
            mmMap.put("total", mmTotal);
            return JsonResult.getSuccessResult(mmMap);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("shijijieguo/getResultCount:error", e);
            return JsonResult.getErrorResult("shijijieguo/getResultCount:error " + e.getMessage());
        }
    }

    @RequestMapping("votesure")
    public JsonResult<?> votesure(@RequestParam("xiangmuid") String ppxiangmuid,
                                  @RequestParam("tiaojian") String pptiaojian,
                                  @RequestParam("userid") String ppuserId) {

        try {

            YltpXiangmu mmxiangmu = ddYltpXiangmuService.findOne(ppxiangmuid);
            if (mmxiangmu.getIsactived() != 1) {
                return JsonResult.getErrorResult("表决失败，投票尚未启动");
            }
            List<YltpRenyuanfenpei> mmfenpei = ddYltprenyuanfenpeiService.findbyUserIDandXiangmuId(ppuserId, ppxiangmuid);
            if (mmfenpei.get(0).getIssubmit() == 1) {
                return JsonResult.getErrorResult("表决失败，投票已提交。");
            }
            if (pptiaojian == null || pptiaojian.isEmpty()) {
                return JsonResult.getErrorResult("请选择筛选条件。");
            }
            String mmleibie = "";
            String mmpinpaiqingkuang = "";
            if (pptiaojian.equals("封闭试剂")) {
                mmleibie = pptiaojian;
            } else if (pptiaojian.equals("一个品牌")) {
                mmpinpaiqingkuang = pptiaojian;
            } else if (pptiaojian.equals("两个品牌")) {
                mmpinpaiqingkuang = pptiaojian;
            } else if (pptiaojian.equals("专机专用")) {
                mmpinpaiqingkuang = pptiaojian;
            }else{
                mmleibie=pptiaojian;
                mmpinpaiqingkuang=pptiaojian;
            }

            List<Map<String, Object>> mmList = ddShijiService.findAllByXiangmuIdAndTiaojian(ppxiangmuid, mmleibie, mmpinpaiqingkuang);
            if (mmList.size() == 0) {
                return JsonResult.getErrorResult("没有筛选出数据。");
            }
            for (Map<String, Object> mmMap : mmList) {
                String mmDetailId = mmMap.get("id").toString();
                String mmPinzhonxuhao = mmMap.get("pinzhongxuhao").toString();
                String mmPinpaixuhao = mmMap.get("pinpai").toString();
                List<Map<String, Object>> mmjieguoList = ddService.findPinguiJieguo(ppxiangmuid, ppuserId, mmDetailId, mmPinzhonxuhao, mmPinpaixuhao);
                /*List<Map<String, Object>> mmdetailList = ddShijiService.findXinghaoByPinpai(ppxiangmuid, mmPinzhonxuhao, mmPinpaixuhao);
                String mmDetailId = mmdetailList.get(0).get("id").toString();*/
                if (mmjieguoList.size() > 0) {
                    String mmId = mmjieguoList.get(0).get("id").toString();
                    Shijihaocaijieguo mmjieguo = ddService.findOne(mmId);
                    mmjieguo.setXiangmuid(ppxiangmuid);
                    mmjieguo.setResult("1");
                    ddService.modify(mmjieguo);
                } else {
                    Shijihaocaijieguo mmjieguo = new Shijihaocaijieguo();
                    String mmId = UUID.randomUUID().toString();
                    mmjieguo.setId(mmId);
                    mmjieguo.setDetailid(mmDetailId);
                    mmjieguo.setUserid(ppuserId);
                    mmjieguo.setXiangmuid(ppxiangmuid);
                    mmjieguo.setResult("1");
                    mmjieguo.setCreatetime(new Date());
                    mmjieguo.setPinzhong(Integer.parseInt(mmPinzhonxuhao));
                    mmjieguo.setPinpai(mmPinpaixuhao);
                    ddService.add(mmjieguo);
                }

            }

            return JsonResult.getSuccessResult("表决成功");

        } catch (Exception e) {
            e.printStackTrace();
            log.error("shijijieguo/votesure:error", e);
            return JsonResult.getErrorResult("shijijieguo/votesure:error " + e.getMessage());
        }
    }
    @RequestMapping("toupiao")
    public JsonResult<?> toupiao(@RequestParam("xiangmuid") String xiangmuid,
                                 @RequestParam("id") List<String> ppdetailIds,
                                 @RequestParam("pinzhong") int ppPinzhong,
                                 @RequestParam("pinpai") String ppPinpai,
                                 @RequestParam("userid") String ppuserId,
                                 @RequestParam("result") String ppresult) {

        try {
            Shijihaocaijieguo mmjieguo = new Shijihaocaijieguo();

            YltpXiangmu mmxiangmu = ddYltpXiangmuService.findOne(xiangmuid);
            if (mmxiangmu.getIsactived() != 1) {
                return JsonResult.getErrorResult("表决失败，投票尚未启动");
            }

            List<YltpRenyuanfenpei> listfenpei = ddYltprenyuanfenpeiService.findbyUserIDandXiangmuId(ppuserId, xiangmuid);
            if (listfenpei.get(0).getIssubmit() == 1) {
                return JsonResult.getErrorResult("表决失败，当前投票项已提交");
            }

                if ("1".equals(ppresult)) {
                        boolean mmIsok = false;
                        for (String mmdetailId : ppdetailIds) {
                            Shijihaocaijieguo Shijihaocaijieguo=ddService.checkexistByUserId(mmdetailId, ppPinzhong, ppPinpai, ppuserId, xiangmuid);
                            if (Shijihaocaijieguo != null) {
                                String mmId = Shijihaocaijieguo.getId();
                                mmjieguo.setId(mmId);
                                mmjieguo.setXiangmuid(xiangmuid);
                                mmjieguo.setResult(ppresult);
                                mmjieguo.setCreatetime(new Date());
                                mmIsok = ddService.modify(mmjieguo);

                            } else {
                                mmjieguo.setId(UUID.randomUUID().toString());
                                mmjieguo.setDetailid(mmdetailId);
                                mmjieguo.setPinzhong(ppPinzhong);
                                mmjieguo.setPinpai(ppPinpai);
                                mmjieguo.setUserid(ppuserId);
                                mmjieguo.setXiangmuid(xiangmuid);
                                mmjieguo.setResult(ppresult);
                                mmjieguo.setCreatetime(new Date());
                                mmIsok = ddService.add(mmjieguo);
                            }
                        }
                        return mmIsok == true ? JsonResult.getSuccessResult("表决成功")
                                : JsonResult.getErrorResult("表决失败");


                } else {
                    boolean mmIsok = false;
                    for (String mmdetailId : ppdetailIds) {
                        Shijihaocaijieguo Shijihaocaijieguo=ddService.checkexistByUserId(mmdetailId, ppPinzhong, ppPinpai, ppuserId, xiangmuid);
                        if (Shijihaocaijieguo != null) {
                            String mmId = Shijihaocaijieguo.getId();
                            mmjieguo.setId(mmId);
                            mmjieguo.setXiangmuid(xiangmuid);
                            mmjieguo.setResult(ppresult);
                            mmjieguo.setCreatetime(new Date());
                            mmIsok = ddService.modify(mmjieguo);
                        } else {
                            mmjieguo.setId(UUID.randomUUID().toString());
                            mmjieguo.setDetailid(mmdetailId);
                            mmjieguo.setPinzhong(ppPinzhong);
                            mmjieguo.setPinpai(ppPinpai);
                            mmjieguo.setUserid(ppuserId);
                            mmjieguo.setXiangmuid(xiangmuid);
                            mmjieguo.setResult(ppresult);
                            mmjieguo.setCreatetime(new Date());
                            mmIsok = ddService.add(mmjieguo);
                        }
                    }
                    return mmIsok == true ? JsonResult.getSuccessResult("表决成功")
                            : JsonResult.getErrorResult("表决失败");
                }
        } catch (Exception e) {
            e.printStackTrace();

            /*log.error("shijijieguo/toupiao:error", e);*/
           /* return JsonResult.getErrorResult("shijijieguo/toupiao:error " + e.getMessage());*/
            return JsonResult.getErrorResult("表决失败");
        }
    }
   /* @RequestMapping("toupiao")
    public JsonResult<?> toupiao(@RequestParam("xiangmuid") String xiangmuid,
                                 @RequestParam("id") List<String> ppdetailIds,
                                 @RequestParam("pinzhong") int ppPinzhong,
                                 @RequestParam("pinpai") String ppPinpai,
                                 @RequestParam("userid") String ppuserId,
                                 @RequestParam("result") String ppresult) {

        try {
            Shijihaocaijieguo mmjieguo = new Shijihaocaijieguo();

            YltpXiangmu mmxiangmu = ddYltpXiangmuService.findOne(xiangmuid);
            if (mmxiangmu.getIsactived() != 1) {
                return JsonResult.getErrorResult("表决失败，投票尚未启动");
            }

            List<YltpRenyuanfenpei> listfenpei = ddYltprenyuanfenpeiService.findbyUserIDandXiangmuId(ppuserId, xiangmuid);
            if (listfenpei.get(0).getIssubmit() == 1) {
                return JsonResult.getErrorResult("表决失败，当前投票项已提交");
            }
            //试剂
            if (("10").equals(mmxiangmu.getXiangmuleixing())) {
                if ("1".equals(ppresult)) {
                    Integer mmpinguishu = ppdetailIds.size();
                    Integer mmVotepinguishu = ddService.getPinguiResultCount(xiangmuid, ppuserId, "1");
                    *//*Integer mmpinguishu=ddShijiService.getCountByPinzhongAndPinpai(ppPinzhong,ppPinpai,xiangmuid).size();*//*
                    Integer mmzongshu = mmVotepinguishu + mmpinguishu;
                    if (mmzongshu > 1300) {
                        return JsonResult.getErrorResult("表决失败，勾选品规数超过1300。");
                    }
                    List<Map<String, Object>> mmPinpaiByPinzhongList = ddService.mmPinpaiByPinzhongList(xiangmuid, ppuserId, ppPinzhong);
                    if (mmPinpaiByPinzhongList.size() > 0) {
                        for (Map<String, Object> mmMap : mmPinpaiByPinzhongList) {
                            String mmpinpai = mmMap.get("pinpai").toString();
                            if (ppPinpai.equals(mmpinpai)) {
                                boolean mmIsok = false;
                                for (String mmdetailId : ppdetailIds) {
                                    Shijihaocaijieguo Shijihaocaijieguo=ddService.checkexistByUserId(mmdetailId, ppPinzhong, ppPinpai, ppuserId, xiangmuid);
                                    if (Shijihaocaijieguo != null) {
                                        String mmId = Shijihaocaijieguo.getId();
                                        mmjieguo.setId(mmId);
                                        mmjieguo.setXiangmuid(xiangmuid);
                                        mmjieguo.setResult(ppresult);
                                        mmjieguo.setCreatetime(new Date());
                                        mmIsok = ddService.modify(mmjieguo);

                                    } else {
                                        mmjieguo.setId(UUID.randomUUID().toString());
                                        mmjieguo.setDetailid(mmdetailId);
                                        mmjieguo.setPinzhong(ppPinzhong);
                                        mmjieguo.setPinpai(ppPinpai);
                                        mmjieguo.setUserid(ppuserId);
                                        mmjieguo.setXiangmuid(xiangmuid);
                                        mmjieguo.setResult(ppresult);
                                        mmjieguo.setCreatetime(new Date());
                                        mmIsok = ddService.add(mmjieguo);
                                    }
                                }
                                return mmIsok == true ? JsonResult.getSuccessResult("表决成功")
                                        : JsonResult.getErrorResult("表决失败");
                            } else {
                                return JsonResult.getErrorResult("同一品种的品牌同意数量不能超过1条。");
                            }
                        }
                        return JsonResult.getErrorResult("表决失败。");
                    } else {
                        boolean mmIsok = false;
                        for (String mmdetailId : ppdetailIds) {
                            Shijihaocaijieguo Shijihaocaijieguo=ddService.checkexistByUserId(mmdetailId, ppPinzhong, ppPinpai, ppuserId, xiangmuid);
                            if (Shijihaocaijieguo != null) {
                                String mmId = Shijihaocaijieguo.getId();
                                mmjieguo.setId(mmId);
                                mmjieguo.setXiangmuid(xiangmuid);
                                mmjieguo.setResult(ppresult);
                                mmjieguo.setCreatetime(new Date());
                                mmIsok = ddService.modify(mmjieguo);

                            } else {
                                mmjieguo.setId(UUID.randomUUID().toString());
                                mmjieguo.setDetailid(mmdetailId);
                                mmjieguo.setPinzhong(ppPinzhong);
                                mmjieguo.setPinpai(ppPinpai);
                                mmjieguo.setUserid(ppuserId);
                                mmjieguo.setXiangmuid(xiangmuid);
                                mmjieguo.setResult(ppresult);
                                mmjieguo.setCreatetime(new Date());
                                mmIsok = ddService.add(mmjieguo);
                            }
                        }
                        return mmIsok == true ? JsonResult.getSuccessResult("表决成功")
                                : JsonResult.getErrorResult("表决失败");
                    }

                } else {
                    boolean mmIsok = false;
                    for (String mmdetailId : ppdetailIds) {
                        Shijihaocaijieguo Shijihaocaijieguo=ddService.checkexistByUserId(mmdetailId, ppPinzhong, ppPinpai, ppuserId, xiangmuid);
                        if (Shijihaocaijieguo != null) {
                            String mmId = Shijihaocaijieguo.getId();
                            mmjieguo.setId(mmId);
                            mmjieguo.setXiangmuid(xiangmuid);
                            mmjieguo.setResult(ppresult);
                            mmjieguo.setCreatetime(new Date());
                            mmIsok = ddService.modify(mmjieguo);
                        } else {
                            mmjieguo.setId(UUID.randomUUID().toString());
                            mmjieguo.setDetailid(mmdetailId);
                            mmjieguo.setPinzhong(ppPinzhong);
                            mmjieguo.setPinpai(ppPinpai);
                            mmjieguo.setUserid(ppuserId);
                            mmjieguo.setXiangmuid(xiangmuid);
                            mmjieguo.setResult(ppresult);
                            mmjieguo.setCreatetime(new Date());
                            mmIsok = ddService.add(mmjieguo);
                        }

                    }
                    return mmIsok == true ? JsonResult.getSuccessResult("表决成功")
                            : JsonResult.getErrorResult("表决失败");
                }
            } else {//医用耗材
                if ("1".equals(ppresult)) {
                    //次关联不能单独勾选
                    List<Map<String, Object>> mmCipinzhongpinpaiList = ddPinpaiguanlianService.findCipinzhongpinpaiList(xiangmuid);
                    for(Map<String, Object> mmMap : mmCipinzhongpinpaiList){
                        String mmcipinzhongxuhao=mmMap.get("cipinzhongxuhao").toString();
                        String mmcipinpai=mmMap.get("cipinpai").toString();
                        if(mmcipinzhongxuhao.equals(String.valueOf(ppPinzhong))&&mmcipinpai.equals(ppPinpai)){
                            return JsonResult.getErrorResult("表决失败，此品种品牌无法单独勾选。");
                        }
                    }
                    //主关联需要查找相关的次关联
                    List<Map<String, Object>> mmPinzhongguanlianList = ddPinpaiguanlianService.findPinzhongguanlianList(xiangmuid);
                    for(Map<String, Object> mmMap : mmPinzhongguanlianList){
                        String mmpinzhongxuhao=mmMap.get("pinzhongxuhao").toString();
                        String mmpinpai=mmMap.get("pinpai").toString();
                        if(mmpinzhongxuhao.equals(String.valueOf(ppPinzhong))&&mmpinpai.equals(ppPinpai)){
                            List<Map<String, Object>> mmCiguanlianList = ddPinpaiguanlianService.findCiguanlianList(xiangmuid,String.valueOf(ppPinzhong),ppPinpai);
                            Integer mmciguanlian= mmCiguanlianList.size();
                            Integer mmpinguishu = ppdetailIds.size();
                            Integer mmVotepinguishu = ddService.getPinguiResultCount(xiangmuid, ppuserId, "1");
                            Integer mmzongshu = mmVotepinguishu + mmpinguishu+mmciguanlian;
                            if (mmzongshu > 6000) {
                                return JsonResult.getErrorResult("表决失败，勾选品规数加关联品规超过6000。");
                            }else{
                                boolean mmIsok = false;
                                for (Map<String, Object> mmCiguanlian : mmCiguanlianList) {
                                    String mmid=mmCiguanlian.get("id").toString();
                                    String pinzhongxuhao=mmCiguanlian.get("pinzhongxuhao").toString();
                                    String pinpai=mmCiguanlian.get("pinpai").toString();
                                    Shijihaocaijieguo Shijihaocaijieguo=ddService.checkexistByUserId(mmid, Integer.valueOf(pinzhongxuhao), ppPinpai, ppuserId, xiangmuid);
                                    if (Shijihaocaijieguo != null) {
                                        String mmId = Shijihaocaijieguo.getId();
                                        mmjieguo.setId(mmId);
                                        mmjieguo.setXiangmuid(xiangmuid);
                                        mmjieguo.setResult(ppresult);
                                        mmjieguo.setCreatetime(new Date());
                                        mmIsok = ddService.modify(mmjieguo);
                                    } else {
                                        mmjieguo.setId(UUID.randomUUID().toString());
                                        mmjieguo.setDetailid(mmid);
                                        mmjieguo.setPinzhong(Integer.valueOf(pinzhongxuhao));
                                        mmjieguo.setPinpai(pinpai);
                                        mmjieguo.setUserid(ppuserId);
                                        mmjieguo.setXiangmuid(xiangmuid);
                                        mmjieguo.setResult(ppresult);
                                        mmjieguo.setCreatetime(new Date());
                                        mmIsok = ddService.add(mmjieguo);
                                    }
                                }
                                List<Map<String, Object>> mmPinpaiByPinzhongList = ddService.mmPinpaiByPinzhongList(xiangmuid, ppuserId, ppPinzhong);
                                if (mmPinpaiByPinzhongList.size() > 1) {
                                    Map<String, Object> mmmappinpai=new HashMap<>();
                                    mmmappinpai.put("pinpai",ppPinpai);
                                    if (mmPinpaiByPinzhongList.contains(mmmappinpai)) {
                                         mmIsok = false;
                                        for (String mmdetailId : ppdetailIds) {
                                            Shijihaocaijieguo Shijihaocaijieguo=ddService.checkexistByUserId(mmdetailId, ppPinzhong, ppPinpai, ppuserId, xiangmuid);
                                            if (Shijihaocaijieguo != null) {
                                                String mmId = Shijihaocaijieguo.getId();
                                                mmjieguo.setId(mmId);
                                                mmjieguo.setXiangmuid(xiangmuid);
                                                mmjieguo.setResult(ppresult);
                                                mmjieguo.setCreatetime(new Date());
                                                mmIsok = ddService.modify(mmjieguo);

                                            } else {
                                                mmjieguo.setId(UUID.randomUUID().toString());
                                                mmjieguo.setDetailid(mmdetailId);
                                                mmjieguo.setPinzhong(ppPinzhong);
                                                mmjieguo.setPinpai(ppPinpai);
                                                mmjieguo.setUserid(ppuserId);
                                                mmjieguo.setXiangmuid(xiangmuid);
                                                mmjieguo.setResult(ppresult);
                                                mmjieguo.setCreatetime(new Date());
                                                mmIsok = ddService.add(mmjieguo);
                                            }
                                        }

                                        return mmIsok == true ? JsonResult.getSuccessResult("表决成功")
                                                : JsonResult.getErrorResult("表决失败");

                                    } else {
                                        return JsonResult.getErrorResult("同一品种的品牌同意数量不能超过2条。");
                                    }
                                } else {

                                    for (String mmdetailId : ppdetailIds) {
                                        Shijihaocaijieguo Shijihaocaijieguo=ddService.checkexistByUserId(mmdetailId, ppPinzhong, ppPinpai, ppuserId, xiangmuid);
                                        if (Shijihaocaijieguo != null) {
                                            String mmId = Shijihaocaijieguo.getId();
                                            mmjieguo.setId(mmId);
                                            mmjieguo.setXiangmuid(xiangmuid);
                                            mmjieguo.setResult(ppresult);
                                            mmjieguo.setCreatetime(new Date());
                                            mmIsok = ddService.modify(mmjieguo);

                                        } else {
                                            mmjieguo.setId(UUID.randomUUID().toString());
                                            mmjieguo.setDetailid(mmdetailId);
                                            mmjieguo.setPinzhong(ppPinzhong);
                                            mmjieguo.setPinpai(ppPinpai);
                                            mmjieguo.setUserid(ppuserId);
                                            mmjieguo.setXiangmuid(xiangmuid);
                                            mmjieguo.setResult(ppresult);
                                            mmjieguo.setCreatetime(new Date());
                                            mmIsok = ddService.add(mmjieguo);
                                        }
                                    }
                                    return mmIsok == true ? JsonResult.getSuccessResult("表决成功")
                                            : JsonResult.getErrorResult("表决失败");
                                }
                            }
                        }
                    }

                    Integer mmpinguishu = ppdetailIds.size();
                    Integer mmVotepinguishu = ddService.getPinguiResultCount(xiangmuid, ppuserId, "1");
                    *//*Integer mmpinguishu=ddShijiService.getCountByPinzhongAndPinpai(ppPinzhong,ppPinpai,xiangmuid).size();*//*
                    Integer mmzongshu = mmVotepinguishu + mmpinguishu;
                    if (mmzongshu > 6000) {
                        return JsonResult.getErrorResult("表决失败，勾选品规数超过6000。");
                    }
                    List<Map<String, Object>> mmPinpaiByPinzhongList = ddService.mmPinpaiByPinzhongList(xiangmuid, ppuserId, ppPinzhong);
                    if (mmPinpaiByPinzhongList.size() > 1) {
                        Map<String, Object> mmpinpai=new HashMap<>();
                        mmpinpai.put("pinpai",ppPinpai);
                        if (mmPinpaiByPinzhongList.contains(mmpinpai)) {
                                boolean mmIsok = false;
                                for (String mmdetailId : ppdetailIds) {
                                    Shijihaocaijieguo Shijihaocaijieguo=ddService.checkexistByUserId(mmdetailId, ppPinzhong, ppPinpai, ppuserId, xiangmuid);
                                    if (Shijihaocaijieguo != null) {
                                        String mmId = Shijihaocaijieguo.getId();
                                        mmjieguo.setId(mmId);
                                        mmjieguo.setXiangmuid(xiangmuid);
                                        mmjieguo.setResult(ppresult);
                                        mmjieguo.setCreatetime(new Date());
                                        mmIsok = ddService.modify(mmjieguo);

                                    } else {
                                        mmjieguo.setId(UUID.randomUUID().toString());
                                        mmjieguo.setDetailid(mmdetailId);
                                        mmjieguo.setPinzhong(ppPinzhong);
                                        mmjieguo.setPinpai(ppPinpai);
                                        mmjieguo.setUserid(ppuserId);
                                        mmjieguo.setXiangmuid(xiangmuid);
                                        mmjieguo.setResult(ppresult);
                                        mmjieguo.setCreatetime(new Date());
                                        mmIsok = ddService.add(mmjieguo);
                                    }
                                }
                                return mmIsok == true ? JsonResult.getSuccessResult("表决成功")
                                        : JsonResult.getErrorResult("表决失败");
                            } else {
                                return JsonResult.getErrorResult("同一品种的品牌同意数量不能超过2条。");
                            }
                    } else {
                        boolean mmIsok = false;
                        for (String mmdetailId : ppdetailIds) {
                            Shijihaocaijieguo Shijihaocaijieguo=ddService.checkexistByUserId(mmdetailId, ppPinzhong, ppPinpai, ppuserId, xiangmuid);
                            if (Shijihaocaijieguo != null) {
                                String mmId = Shijihaocaijieguo.getId();
                                mmjieguo.setId(mmId);
                                mmjieguo.setXiangmuid(xiangmuid);
                                mmjieguo.setResult(ppresult);
                                mmjieguo.setCreatetime(new Date());
                                mmIsok = ddService.modify(mmjieguo);

                            } else {
                                mmjieguo.setId(UUID.randomUUID().toString());
                                mmjieguo.setDetailid(mmdetailId);
                                mmjieguo.setPinzhong(ppPinzhong);
                                mmjieguo.setPinpai(ppPinpai);
                                mmjieguo.setUserid(ppuserId);
                                mmjieguo.setXiangmuid(xiangmuid);
                                mmjieguo.setResult(ppresult);
                                mmjieguo.setCreatetime(new Date());
                                mmIsok = ddService.add(mmjieguo);
                            }
                        }
                        return mmIsok == true ? JsonResult.getSuccessResult("表决成功")
                                : JsonResult.getErrorResult("表决失败");
                    }

                } else {
                    boolean mmIsok = false;
                    for (String mmdetailId : ppdetailIds) {
                        Shijihaocaijieguo Shijihaocaijieguo=ddService.checkexistByUserId(mmdetailId, ppPinzhong, ppPinpai, ppuserId, xiangmuid);
                        if (Shijihaocaijieguo != null) {
                            String mmId = Shijihaocaijieguo.getId();
                            mmjieguo.setId(mmId);
                            mmjieguo.setXiangmuid(xiangmuid);
                            mmjieguo.setResult(ppresult);
                            mmjieguo.setCreatetime(new Date());
                            mmIsok = ddService.modify(mmjieguo);
                        } else {
                            mmjieguo.setId(UUID.randomUUID().toString());
                            mmjieguo.setDetailid(mmdetailId);
                            mmjieguo.setPinzhong(ppPinzhong);
                            mmjieguo.setPinpai(ppPinpai);
                            mmjieguo.setUserid(ppuserId);
                            mmjieguo.setXiangmuid(xiangmuid);
                            mmjieguo.setResult(ppresult);
                            mmjieguo.setCreatetime(new Date());
                            mmIsok = ddService.add(mmjieguo);
                        }
                    }
                    return mmIsok == true ? JsonResult.getSuccessResult("表决成功")
                            : JsonResult.getErrorResult("表决失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("shijijieguo/toupiao:error", e);
            return JsonResult.getErrorResult("shijijieguo/toupiao:error " + e.getMessage());
        }
    }*/

    //  导出某个人员的签字表
    @RequestMapping("qianzibiao")
    public void qianzibiao(HttpServletRequest request, HttpServletResponse response,
                           @RequestParam("xiangmuid") String ppxiangmuid,
                           @RequestParam("xiangmuleixing") String ppxiangmuleixing,
                           @RequestParam("userid") String ppuserid) {
        try {
            ddService.daochuqianzibiao(ppxiangmuid, ppuserid, request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("findHuizongbiaolist")
    public JsonResult<?> findHuizongbiaolist(@RequestParam("xiangmuid") String ppxiangmuid) {
        try {
            return ddService.findHuizongbiaolist(ppxiangmuid);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("jieguo/findHuizongbiaolist:error", e);
            return JsonResult.getErrorResult("jieguo/findHuizongbiaolist:error " + e.getMessage());
        }


    }

    @RequestMapping("huizongbiao")
    public void huizongbiao(HttpServletRequest request, HttpServletResponse response,
                            @RequestParam("xiangmuid") String ppxiangmuid,
                            @RequestParam("xiangmuleixing") String ppxiangmuleixing) {

        ddService.huizongbiao(request, response, ppxiangmuid, ppxiangmuleixing);
    }
}
